package com.coder.shop.web.controller;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coder.shop.dao.model.BaseMsgCode;
import com.coder.shop.dao.model.UcenterUserDetailsExample;
import com.coder.shop.dao.model.UcenterUserDetailsExample.Criteria;
import com.coder.shop.rpc.api.MsgCodeService;
import com.coder.shop.rpc.api.UcenterUserDetailsService;
import com.coder.shop.rpc.api.UcenterUserService;

@Controller
@RequestMapping("reg")
public class RegisterController {
	
	@Resource
	private UcenterUserDetailsService ucenterUserDetailsService;
	
	@Resource
	private UcenterUserService usercenterUserService;
	
	@Resource
	private MsgCodeService msgCodeService;

	@RequestMapping
	public String page() {
		return "login";
	}
	
	@RequestMapping(value="register",method=RequestMethod.POST)
	@ResponseBody
	public String register(String phone,String smscode,String password) {
		
		if(StringUtils.isEmpty(phone)) {
			return "手机号不能为空";
		}
		
		if(StringUtils.isEmpty(smscode)) {
			return "验证码不能为空";
		}
		
		if(StringUtils.isEmpty(password)) {
			return "密码不能为空";
		}
		
		//判断验手机号是否被注册
        UcenterUserDetailsExample example = new UcenterUserDetailsExample();
        Criteria createCriteria = example.createCriteria();
        createCriteria.andPhoneEqualTo(phone);
        if(ucenterUserDetailsService.selectFirstByExample(example)!=null) {
        	return "手机号已存在";
        }
		
		//判断验证码是否正确
        BaseMsgCode msgCode = msgCodeService.selectByCodeAndPhone(phone, smscode, BaseMsgCode.STATUS_NO_USE);
        if(msgCode == null) {
        	return "验证码错误";
        }
        
		//调用注册服务
        usercenterUserService.register(phone, smscode, password);
		
        return "0";
	}
	
	
	
	
	@RequestMapping("sendMsgCode")
	@ResponseBody
	public String sendMsgCode(String phone) {
		
		if(StringUtils.isEmpty(phone)) {
			return "手机号不能为空";
		}
		//判断格式
		String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if(phone.length() != 11){
            System.out.println("手机号应为11位数");
            return "手机号应为11位数";
        }else{
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if(isMatch){
                System.out.println("您的手机号" + phone + "是正确格式@——@");
            } else {
                System.out.println("您的手机号" + phone + "是错误格式！！！");
                return "手机号格式错误";
            }
        }
        
		//是否已经被注册
        UcenterUserDetailsExample example = new UcenterUserDetailsExample();
        Criteria createCriteria = example.createCriteria();
        createCriteria.andPhoneEqualTo(phone);
        if(ucenterUserDetailsService.selectFirstByExample(example)!=null) {
        	return "手机号已存在";
        }
		
		//调用发送服务
        msgCodeService.sendMsgCode(phone);
        
		
        return "0";
	}
	
	
}
